import "dart:ffi";

import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/collect/api.dart";
import "package:ct_pa_majordomo/pages/device/collect/dict.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Detail extends StatefulWidget {
  final int collectId;
  const Detail({super.key, required this.collectId});

  @override
  State<StatefulWidget> createState() => _DetailState();

}

class _DetailState extends State<Detail> {
  final TextEditingController _controller = TextEditingController();

  Map _info = {};

  void _editName() async {
    _controller.text = _info["name"];
    taiDialog(
      context: context,
      title: "采集器名称",
      contentPadding: const EdgeInsets.symmetric(horizontal: ThemeVars.paddingPage, vertical: 20),
      onConfirm: (taiContext) {
        setState(() {
          _info["name"] = _controller.text;
        });
        Navigator.of(taiContext).pop();
      },
      child: TextFormField(
        controller: _controller,
        textAlign: TextAlign.center,
        decoration: const InputDecoration(
          hintText: "请输入",
          hintStyle: TextStyle(color: ThemeVars.colorTextTer),
          border: InputBorder.none,
          filled: true,
          fillColor: ThemeVars.colorBackground,
        ),
      ),
    );
  }

  Future<void> _getCollectInfo() async {
    print("采集器信息${widget.collectId}");
    Map resData = await collectApi.getCollectInfo(widget.collectId);
    setState(() {
      _info = resData["data"] ?? {};
    });
  }

  void _removeCollect(BuildContext taiContext) async {
    EasyLoading.show();
    await collectApi.removeCollect(widget.collectId);
    EasyLoading.dismiss();
    if (taiContext.mounted) {
      Navigator.of(taiContext).pop();
      Stacker.pop(true);
    }
  }

  @override
  void initState() {
    _getCollectInfo();
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: _info["name"] ?? "",
      child: ListView(
        children: [
          MenuBlock(
            items: [
              MenuBlockItem(
                text: "采集器名称",
                suffix: Text(_info["name"] ?? ""),
                onTap: () {
                  _editName();
                }
              ),
              MenuBlockItem(
                text: "状态",
                showArrow: false,
                suffix: Text(CollectDict().collectStatus[_info["status"]] ?? ""),
              ),
              MenuBlockItem(
                text: "编码",
                showArrow: false,
                suffix: Text(_info["number"] ?? ""),
              ),
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            items: [
              MenuBlockItem(
                text: "绑定的电表",
                suffix: Text(_info["bindTotal"] ?? "0"),
                onTap: () {
                  Stacker.pushNamed(Keys.DEVICE_COLLECT_BIND_AMMETER, arguments: _info["number"]);
                }
              ),
            ],
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(ThemeVars.paddingPage, 40, ThemeVars.paddingPage, 0),
            child: Button(
              type: ButtonType.danger,
              onPressed: () {
                taiDialog(
                  context: context,
                  child: const Text("确定删除吗？", textAlign: TextAlign.center, style: TextStyle(fontSize: 16),),
                  onConfirm: (taiContext) {
                    _removeCollect(taiContext);
                  }
                );
              },
              child: const Text("删除"),
            ),
          )
        ],
      ),
    );
  }

}